package com.caoaman.sharding.utils.shardingarithmetic;

import com.caoaman.sharding.utils.DateUtil;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;

import java.util.Collection;

/**
 * @ClassName: PreciseTableShardingAlgorithm
 * @Description: 表精确分片算法
 * @Author: CaoAMan
 * @Create: 2021-12-18 16:03
 */
public class PreciseTableShardingAlgorithm implements PreciseShardingAlgorithm<Long> {


    /**
     * 精确分片算法
     *
     * @param availableTargetNames 所有配置的表列表，这里代表所匹配到库的所有表
     * @param shardingValue        分片值，也就是save_time_com的值
     * @return 所匹配表的结果
     */
    @Override
    public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<Long> shardingValue) {

        long value = shardingValue.getValue();

        if (value <= 0) {
            throw new UnsupportedOperationException("preciseShardingValue is null");
        }

        final String yearJoinMonthStr = DateUtil.getYearJoinMonthByMillisecond(value);
        for (String availableTargetName : availableTargetNames) {
            if (availableTargetName.endsWith(yearJoinMonthStr)) {
                return availableTargetName;
            }
        }
        throw new UnsupportedOperationException();
    }

}
